package com.desay.pangoo.itmt.service.impl;

import java.util.List;
import java.util.Optional;

import com.desay.pangoo.itmt.controller.vo.TechOnlineReviewResponseVo;
import com.desay.pangoo.itmt.controller.vo.TechProjectResponseVo;
import com.desay.pangoo.itmt.service.TechProcessService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.desay.pangoo.itmt.entity.TechOnlineReviewEntity;
import com.desay.pangoo.itmt.entity.TechProjectInfoEntity;
import com.desay.pangoo.itmt.repository.TechOnlineReviewRepository;
import com.desay.pangoo.itmt.repository.TechProjectInfoRepository;
import com.desay.pangoo.itmt.repository.spec.ExpandCriteria;
import com.desay.pangoo.itmt.repository.spec.Restrictions;
import org.springframework.stereotype.Service;

/**
 * @author uidq1163
 */
@Service
public class TechProcessImpl implements TechProcessService {

	@Autowired
	TechProjectInfoRepository techProjectInfoRepository;

	@Autowired
	TechOnlineReviewRepository techOnlineReviewRepository;

	/**
	 * 根据技术课题获取技术评审、技术定义任务书、技术成果物、子技术
	 * 
	 * @param techProcessId
	 */
	@Override
	public Object getTechProcess(Long techProcessId) {
		TechProjectResponseVo techProjectResponseVo = new TechProjectResponseVo();
		Optional<TechProjectInfoEntity> techOptional = techProjectInfoRepository.findById(techProcessId);
		if (techOptional.isPresent()) {
			TechProjectInfoEntity tech = techOptional.get();
			BeanUtils.copyProperties(tech, techProjectResponseVo);
			// 子技术
			List<TechProjectInfoEntity> subList = techProjectInfoRepository
					.findByTechNameAndTechPtnLikeOrderByTechPtnDesc(tech.getTechName(), tech.getTechPtn().concat("%"));
			List<TechProjectResponseVo> subVoList = subList.stream().filter(sub -> !sub.getId().equals(tech.getId()))
					.map(sub -> {
						TechProjectResponseVo subVo = new TechProjectResponseVo();
						BeanUtils.copyProperties(sub, subVo);
						return subVo;
					}).toList();
			techProjectResponseVo.setSubList(subVoList);
			// 获取技术评审、技术定义任务书、技术成果物
			ExpandCriteria<TechOnlineReviewEntity> c = new ExpandCriteria<>();
			c.add(Restrictions.eq("techProjects.id", tech.getId(), true));
			List<TechOnlineReviewEntity> listView = techOnlineReviewRepository.findAll(c);
			List<TechOnlineReviewResponseVo> viewVoList = listView.stream().map(sub -> {
				TechOnlineReviewResponseVo subVo = new TechOnlineReviewResponseVo();
				BeanUtils.copyProperties(sub, subVo);
				return subVo;
			}).toList();
			techProjectResponseVo.setListView(viewVoList);
		}
		return techProjectResponseVo;
	}
}
